﻿using CleanArchitecture.Core.Handlers;
using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Infrastructure.Entities.Settings;
using CleanArchitecture.Infrastructure.Interfaces;
using CleanArchitecture.Infrastructure.Interfaces.Business;
using CleanArchitecture.Infrastructure.Models;
using CleanArchitecture.Infrastructure.Models.Business.Columns;
using CleanArchitecture.Web.Factories;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;

namespace CleanArchitecture.Web.Controllers.Frontend
{
    public class HomeController : BaseFrontendController
    {
        #region Field
        private readonly IColumnService _columnService;
        #endregion

        #region　Ctrl
        public HomeController(IColumnService columnService,
            IBusinessModelFactory businessModelFactory)
            : base(businessModelFactory)
        {
            _columnService = columnService;
        }

        #endregion
        public IActionResult Index()
        {
            var commonSettings = EngineContext.Current.Resolve<CommonSettings>();
            if (string.IsNullOrEmpty(commonSettings.ServerUrl) || commonSettings.ServerUrl == "/")
            {
                var webHelper = EngineContext.Current.Resolve<IWebHelper>();
                commonSettings.ServerUrl = webHelper.GetServerLocation();
                EngineContext.Current.Resolve<ISettingService>().SaveSetting(commonSettings);
            }
            IList<ColumnItemModel> allColumns = _columnService.GetColumnList();
            var homecolumn = allColumns.FirstOrDefault(c => c.Level == 1 && c.ParentId == 0 && c.UrlRoute == "/");
            ViewBag.AllColumns = allColumns;
            ViewBag.CurrentColumn = homecolumn;
            return View();
        }

        public IActionResult Error()
        {
            return View();
        }

        public IActionResult Login()
        {
            var model = new LoginModel();
            model.RememberMe = true;
            return View(model);
        }
    }
}
